<?php
//模型
class UserLoginModel extends MY_Model {
	function __construct(){
		parent::__construct();
		$this->table = 'user_login';
		$this->err_msg = '';
	}
	//分页列表
	function get_list($arr=array(),$page=1,$size=10){		
		$sql  = "select * from ".$this->db->dbprefix.$this->table." LIMIT ".($page-1)*$size .",{$size}";
		$query = $this->db->query($sql);
		$res = $query->result_array();		
		
		$sql  = "select count(*) as num from ".$this->db->dbprefix.$this->table;
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return array('list'=>$res,'count' =>$row['num']);		
	}
	//全部列表
	function get_lists(){		
		$sql  = "select * from ".$this->db->dbprefix.$this->table;
		$query = $this->db->query($sql);
		$res = $query->result_array();
		return $res;		
	}
	//详情
	function get_info($id){
		$sql  = "select * from ".$this->db->dbprefix.$this->table." where id='{$id}' ";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return $row;
	}
	//唯一条件获取详情
	function unique_get_info($field,$value){
		$sql  = "select * from ".$this->db->dbprefix.$this->table." where {$field}='{$value}' ";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return $row;
	}
	//TOKEN获取登录信息
	public function token_get_data($token){
		$query = $this->db->get_where($this->table, array('token' => $token));
		$row = $query->row_array();
		if(!$row)return false;
		return $row;
	}
	
	//登录 $code -> 小程序登录时获取的code
	public function login($openid,$mobile){
		if(empty($openid)){
			return false;
		}
		
		$time = time();	
		$this->load->model('User/UserModel', 'user');
		$info = $this->user->unique_get_info('openid',$openid);
		
		$abled = 0;//是否开启新用户验证
		if(!$info){
			$info = $this->user->unique_get_info('mobile',$mobile);
			if($info){
				$user_id = $info['id'];
				$abled	 =	$info['disabled'];
				$this->user->edit(array('openid'=>$openid),"id='{$user_id}'");
			}
			else{
				$this->load->model('Admin/ConfigModel', 'configs');
				$disabled = $this->configs->unique_get_info("name","user_examine");
			
				if($disabled['info']==1){
					$abled = 1;//需要验证
				}
				else{
					$abled = 0;//不需要验证
				}
				$uinfo = array(
					'openid' => $openid,
					'addtime'=> $time,
					'disabled'=> $abled,
					'mobile'=> $mobile
				);
				
				if(empty($mobile)){
					$this->err_msg = '手机号码不能为空';
					return false;
				}
				
				$is_exist = $this->user->_where_exist("mobile='{$uinfo['mobile']}' AND openid!=''");
				if($is_exist){
					$this->err_msg = '手机号码已存在';
					return false;
				}
				//写入新用户
				$user_id = $this->user->add($uinfo);
			}
			
		}else{
			$user_id = $info['id'];
			
			if(empty($info['mobile'])){
				$this->user->edit(array('mobile'=>$mobile),"id='{$user_id}'");
			}
			else{
				$this->err_msg = '已绑定手机号码';
				return false;
			}
			//非新用户直接看user用户表
			$abled	=	$info['disabled'];
		}
		
		$arr = array("user_id"=>$user_id,"disabled"=>$abled);
		return $arr;
	}
	
	function getErr(){
		return $this->err_msg;
	}
	//创建登录token
	public function create_token($uid=0){
		$time = time();		
		$token = md5($uid.$time.rand(1000,9999));
		
		$data = array(
			'token' 	=> $token,
			'addtime' 	=> $time,
			'user_id'	=> $uid
		);
		
		$row = $this->getRow(array("user_id"=>$uid));
		if($row){
			$this->del("user_id= '{$uid}' ");
		}
		
		$this->db->insert($this->table, $data);
		return $token;
	}	
	//获取小程序用户openid
	public function get_openid($code=0){
		if(empty($code)){
			return false;
		}
		
		$this->load->model('Admin/ConfigModel', 'configs','index');
		$AppID = $this->configs->unique_get_info("name","UserAppID");
		$AppID = $AppID['info'];
		$AppSecret = $this->configs->unique_get_info("name","UserAppSecret");
		$AppSecret = $AppSecret['info'];
		
		$url = "https://api.weixin.qq.com/sns/jscode2session?appid=".$AppID."&secret=".$AppSecret."&js_code=".$code."&grant_type=authorization_code";
		
		$union = @file_get_contents($url);
		
		if(empty($union)){
			return false;
		}
		
		$union = json_decode($union,true);		
		//var_dump($union);exit;
		if(isset($union['errcode'])){
			return false;
		}
		return $union['openid'];
	}
	
	
	
}

